package com.jsgt.greekfire.ui.pool.incomerecord

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.PAGE_SIZE
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.NetDataManager
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.ui.pool.incomerecord.adapter.IncomeRecordAdapter
import com.jsgt.greekfire.viewmodel.CoinViewModel
import com.jsgt.greekfire.common.widget.LoadPageHolder
import com.jsgt.greekfire.common.widget.RecyclerViewItemDecoration
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.activity_recyclerview.*

/**
 * 收益记录
 */

class IncomeRecordListActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }
    private var currentPage = 0//从1开始获取
    private var isRefresh = true
    private var noMore = false//没有更多
    private lateinit var loadPageHolder: LoadPageHolder
    override fun getLayoutId() = R.layout.activity_recyclerview
    var adapter: IncomeRecordAdapter? = null
    override fun main() {
        super.main()
        setTitle(R.string.income_record)
        val decoration = RecyclerViewItemDecoration.Builder(this)
            .color(0) //分割线颜色
            .thickness(resources.getDimensionPixelSize(R.dimen.divider)) //这里是分割线高度
            .firstLineVisible(true)
            .lastLineVisible(true)
            .create()
        recycler_view.addItemDecoration(decoration)
        adapter = IncomeRecordAdapter(NetDataManager.walletInfo?.walletLevel ?: 0 > 0)
        recycler_view.adapter = adapter
        coinViewModel.incomeRecordLiveData.observe(this, Observer {
            if (it != null) {
                if (isRefresh) {
                    adapter?.refreshData(it)
                } else {
                    adapter?.addData(it)
                }
            }
            if (isRefresh) {
                refresh_layout.finishRefresh()
                refresh_layout.finishLoadMore()
                if (it != null && it.isNotEmpty()) {
                    currentPage = 1
                } else {
                    noMore = true
                    refresh_layout.finishLoadMoreWithNoMoreData()
                }
            } else {
                if (it != null && it.isNotEmpty()) {
                    currentPage++
                    noMore = false
                } else {
                    noMore = true
                }
                if (noMore) {
                    refresh_layout.finishLoadMoreWithNoMoreData()
                } else {
                    refresh_layout.finishLoadMore()
                }
            }
            if(adapter!!.dataList().isEmpty()) {
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.EMPTY)
            }else{
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.SUCCESS)
                refresh_layout.setEnableLoadMore(true)
            }
        })

        coinViewModel.errorLiveData.observe(this, Observer {
            if(adapter!!.dataList().isEmpty()) {
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.EMPTY)
            }else{
                loadPageHolder.setLoadState(LoadPageHolder.LoadState.SUCCESS)
            }
            if (isRefresh) {
                refresh_layout.finishRefresh()
            }else{
                refresh_layout.finishLoadMore()
            }
        })

        val materialHeader = MaterialHeader(this)
        materialHeader.setColorSchemeResources(R.color.black)
        refresh_layout.setRefreshHeader(materialHeader)
        refresh_layout.setOnRefreshListener {
            initData()
        }
        refresh_layout.setOnLoadMoreListener {
            isRefresh = false
            if (currentPage > 0) {
                coinViewModel.getIncomeRecord(currentPage + 1, PAGE_SIZE)
            } else {
                //第一页
                initData()
            }
        }
        refresh_layout.setEnableLoadMore(false)
        loadPageHolder = LoadPageHolder(fl_fragment_container)
        loadPageHolder.setLoadState(LoadPageHolder.LoadState.LOADING)
        loadPageHolder.setBackgroundClickEnable(true)
        initData()
    }

    private fun initData() {
        isRefresh = true
        noMore = false
        coinViewModel.getIncomeRecord(1, PAGE_SIZE)
    }
}